package com.thti.mecool.client.model.biz;

import java.util.List;

import android.content.Context;
import android.database.Cursor;

import com.threeti.mecool.mobile.interfaces.dto.ProjectWithStoresAndSellIntoDto;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.db.DataBase;
import com.thti.mecool.client.model.po.Project;
import com.thti.mecool.client.model.po.Salers;
import com.thti.mecool.client.model.po.SalersProject;
import com.thti.mecool.client.model.po.Task;

public class SalersBizImpl {
	
	private DataBase dataBase;

	public SalersBizImpl(Context context) {
		dataBase = new DataBase(context);
	}
	public void save(Salers salers){
		String sqlString="insert into salers(storeId,name,phone,idCardNo,onDutyTime,offTime) values(?,?,?,?,?,?)";
		dataBase.execObject(sqlString, new String[]{salers.getStoreId(),salers.getName(),salers.getPhone(),salers.getIdCardNo(),salers.getOnDutyTime(),salers.getOffTime()});
		dataBase.close();
	}
	
	
	public void update(Salers salers){
		String sqlString="update salers set storeId=?,name=?,phone=?,onDutyTime=?,offTime=? where idCardNo=?";
		dataBase.execObject(sqlString, new String[]{salers.getStoreId(),salers.getName(),salers.getPhone(),salers.getOnDutyTime(),salers.getOffTime(),salers.getIdCardNo()});
		dataBase.close();
	}
	
	/**
	 * 判断projrctId是已经存在
	 * @param id
	 * @return
	 */
	public boolean findSalerId(String storeId,String idCardNo){
		String sqlString="select  * from salers where storeId=? and idCardNo=?";
		Cursor cursor=dataBase.queryObject(sqlString, new String[]{storeId,idCardNo});
		
		if(cursor.moveToNext()){
			boolean flag = cursor.getLong(0)>0 ?true:false;
			cursor.close();
			dataBase.close();
			return flag;
		}
		cursor.close();
		dataBase.close();
		return false;

	} 
		
	@SuppressWarnings("unchecked")
	public List<Salers> findAll(String storeId){
		// select pro.planId,p.planName,p.userId,pro.projectName,t.taskId,t.taskName,t.storeId,t.storeName,t.startTime,t.endTime from plan p,project pro,task t where p.id=pro.planId AND pro.id=t.projectId
		String sql="select  * from  salers  where storeId=?";
		List<Salers> list=dataBase.findAll(Salers.class, sql, new String[]{storeId});
		dataBase.close();
		return list;
		
	}
	
	public void delete(String idCardNo){
		String sqlString="delete from  salers where idCardNo=?";
		dataBase.execObject(sqlString, new String[]{idCardNo});
		dataBase.close();
	}

}
